import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;


public class RandomWalk {
	static Point[] path;
	static ImageProcessing imp = new ImageProcessing(1);
		
	public RandomWalk(){}
	
	public static void main(String[] args) throws IOException{
		
		Color colorToFind = new Color(254,0,0);
		
		Vector<Point> pixel = imp.pixelSearch(colorToFind,imp.readImage());
		
		int[][] maps = imp.convertToMap();
		imp.writeCSV();
		//printMap(maps);
		
		Point start = getStartPoint(maps,pixel);
		int numSteps = 3600;
		Point end = walkRandomly(numSteps,start, maps);
		
		BufferedImage image = imp.image;
		writeImage(image,maps);
		
		System.out.println(end.x+", "+end.y);
		if(pixel.contains(end)) System.out.println("contain");
		
	}
	//
	public static void writeImage(BufferedImage image, int[][] map) throws IOException{
		int w = image.getWidth();
        int h = image.getHeight(); 
		BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Color red = new Color(254,0,0);
		int redColor = red.getRGB();
		Color white = new Color(255,255,255);
		int whiteColor=  white.getRGB();
		for(int i=0;i<map.length;i++){
			for(int j=0;j<map[0].length;j++){
				if(map[i][j]==1) bufferedImage.setRGB(i, j, redColor);
			}
		}
		for(int i=0;i<path.length;i++){
			int x=path[i].x;
			int y=path[i].y;
			
			bufferedImage.setRGB(x,y,whiteColor);
		}
		ImageIO.write(bufferedImage,"jpg",new File("C:/Users/mengdi/Desktop/material/new.jpg"));
				
	}
   //
	
	public static Point getStartPoint(int[][] map, Vector<Point> pixels){
		/*1. start from the topmost*/
		/*
		  for(int i=0;i<map.length;i++){
			for(int j=0;j<map[0].length;j++){
				if(map[i][j]==1) return new Point(i,j);
			}
		}
		return new Point(0,0);
		*/
		/*2. start randomly*/
		
		Random rand = new Random();
		int size = pixels.size();
		int index = rand.nextInt(size);
		return pixels.get(index);
		
		/*3.start from picked point*/
		//return new Point(824,1067);
		
		
		
		
		
	}
	public static Point walkRandomly(int numSteps, Point start, int[][] map) throws IOException{
		int x=start.x;
		int y=start.y;
		int xBound = map.length;
		int yBound = map[0].length;
		int count = 0;
		int option = 0;
		
		File file = new File( "C:/Users/mengdi/Desktop/material/path.txt");  
        if ( !file.exists() ) file.createNewFile();
    	FileWriter writer = new FileWriter(file);
		
    	writer.append("("+start.x+","+start.y+")-->");
    	
    	path = new Point[numSteps+1];
    	path[0]=start;
    	
    	Random rand = new Random();
		while(count<numSteps){
			option = rand.nextInt(8);
		    switch(option){
		    case 0:
		    	if((x >= 1)&&(map[x-1][y]==1)) x--; 
		    	break;
		    case 1:
		    	if((y >= 1)&&(map[x][y-1]==1)) y--; 
		    	break;
		    case 2:
		    	if((x < xBound - 1)&&(map[x+1][y]==1)) x++; 
		    	break;
		    case 3:
		    	if((y < yBound - 1)&&(map[x][y+1]==1)) y++; 
		    	break;
		    case 4:
		    	if((x>=1)&&(y>=1)&&(map[x-1][y-1]==1)){ x--; y--;}
		    	break;
		    case 5:
		    	if((x>=1)&&(y<yBound-1)&&(map[x-1][y+1]==1)){ x--; y++;} 
		    	break;
		    case 6:
		    	if((x<xBound-1)&&(y>=1)&&(map[x+1][y-1]==1)){ x++; y--;}
		    	break;
		    case 7:
		    	if((x<xBound-1)&&(y<yBound-1)&&(map[x+1][y+1]==1)){ x++; y++;}
		    	break;
		    }
			if (map[x][y]==1) count++;
			path[count]=new Point(x,y);
			//print the path
			if(count%10==0) writer.append("\n");
			writer.append("("+x+","+y+")-->");
			
		}
	    Point end = new Point (x,y);
	    writer.append("end");
	    writer.flush();
	    writer.close();
		return end;	
		}
	public static void printMap(int[][] maps) throws IOException{
		File file = new File( "C:/Users/mengdi/Desktop/material/map.txt");  
        if ( !file.exists() ) file.createNewFile();
    	FileWriter writer = new FileWriter(file);
    	for(int i=0;i<maps.length;i++){
			for(int j=0;j<maps[i].length;j++){
				writer.append(maps[i][j]+" ");
			}
			writer.append("\n");
		}
    	    writer.flush();
    	    writer.close();
		
		
	}
}
